package org.phantom.securityframework.api.factory;

/**
 * 获取工厂对象
 * @author david
 *
 */
public class ManagerFactoryBean {

	public static ManagerFactory getFactory(String name){
		return ManagerFactoryBean.getFactory(name, null);
	}
	public static ManagerFactory getFactory(Class<ManagerFactory> type){
		return getFactory(type.getName(),null);
	}
	public static ManagerFactory getFactory(Class<ManagerFactory> type,ClassLoader loader){
		return getFactory(type.getName(),null);
	}
	@SuppressWarnings("rawtypes")
	public static ManagerFactory getFactory(String name,ClassLoader loader){
		if(loader==null){
			loader = Thread.currentThread().getContextClassLoader();
		}
		try {
			Class c = loader.loadClass(name);
			if(isInstanceOf(c))
				return (ManagerFactory) c.newInstance();
			return null;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("rawtypes")
	private static boolean isInstanceOf(Class clazz){
		for (Class c : clazz.getInterfaces()) {
			if(c.getName().equals("org.phantom.securityframework.api.factory.ManagerFactory"))
				return true;
		}
		return false;
	}
}
